package com.shopping.huaju.commodity.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.shopping.huaju.commodity.entity.vo.SpuSaveVO;
import com.shopping.huaju.commodity.feign.CouponFeignService;
import com.shopping.huaju.commodity.service.SpuInfoDescService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shopping.huaju.commodity.entity.SpuInfoEntity;
import com.shopping.huaju.commodity.service.SpuInfoService;
import com.shopping.huaju.common.utils.PageUtils;
import com.shopping.huaju.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品 spu 信息
 *
 * @author tofu
 * @email tofu@qq.com
 * @date 2023-11-09 13:21:12
 */
@RestController
@RequestMapping("commodity/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    @Resource
    private SpuInfoDescService spuInfoDescService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = spuInfoService.queryPage(params);//只带分页条件，没有带查询条件
        PageUtils page = spuInfoService.queryPageByConditions(params);//带查询喝分页条件
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);
        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存 Spu 商品所有信息
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:spuinfo:save")
    public R save(@RequestBody SpuSaveVO saveVO){
		//spuInfoService.save(spuInfo);
        //添加 Spu基本信息
        spuInfoService.saveSpuInfo(saveVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据skuId查询spu的信息
     * @param skuId
     * @return
     */
    @GetMapping(value = "/skuId/{skuId}")
    public R getSpuInfoBySkuId(@PathVariable("skuId") Long skuId) {

        SpuInfoEntity spuInfoEntity = spuInfoService.getSpuInfoBySkuId(skuId);

        return R.ok().setData(spuInfoEntity);
    }


    /**
     * 修改商品发布状态为 上架状态
     */
    @RequestMapping("/{spuId}/up")
    public R updatePublishStatusUp(@PathVariable Long spuId){
        spuInfoService.updatePublishStatusUp(spuId);
        return R.ok();
    }

    /**
     * 修改商品发布状态为 下架状态
     */
    @RequestMapping("/{spuId}/down")
    public R updatePublishStatusDown(@PathVariable Long spuId){
        spuInfoService.updatePublishStatusDown(spuId);
        return R.ok();
    }
}
